package com.dfrz.bookstore.dao.impl;

import com.dfrz.bookstore.dao.Dao;
import com.dfrz.bookstore.entry.BookType;
import com.dfrz.bookstore.entry.Order;
import com.dfrz.bookstore.util.JdbcUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 类功能描述:
 *
 * @Author keyan
 * @Date 2021/5/20 19:39
 */
public class BookTypeDaoImpl implements Dao<BookType> {
    @Override
    public boolean insert(BookType bookType)   {
        String sql = "insert into book_type (type) values (?)";
        PreparedStatement prep = null;
        Connection conn = null;
        try {
            conn = JdbcUtils.getConnection();
            prep = conn.prepareStatement(sql);
            prep.setString(1,bookType.getType());
            int i = prep.executeUpdate();
            if (i>0){
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                JdbcUtils.close(conn,prep,null);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean deleteObjById(int i) {
        String sql = "delete from book_type where id = "+i ;
        Connection conn = null;
        Statement stat;
        try {
            conn = JdbcUtils.getConnection();
            stat = conn.createStatement();
            int i1 = stat.executeUpdate(sql);
            if (i1>0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateByObj(BookType bookType) {
        if (bookType.getId()==0){
            return false;
        }
        StringBuilder sb = new StringBuilder("update book_type set id = "+bookType.getId());
        if (bookType.getType()!=null){
            sb.append(" , type = '"+bookType.getType()+"' ");
        }
        sb.append(" where id = "+bookType.getId());
        Connection conn = null;
        Statement stat = null;
        try {
            conn = JdbcUtils.getConnection();
            stat =  conn.createStatement();
            int i = stat.executeUpdate(sb.toString());
            if (i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                JdbcUtils.close(conn,stat,null);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public List<BookType> findObjByObj(BookType bookType) {
        List<BookType> bookTypes = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select * from book_type where 1=1 ");
        if (bookType.getId()!= 0){
            sb.append(" and id = "+bookType.getId());
        }
        if (bookType.getType()!=null){
            sb.append(" and type = '"+bookType.getType()+"' ");
        }
        Connection conn = null;
        Statement stat = null;
        ResultSet resultSet =null;
        try {
            conn= JdbcUtils.getConnection();
            stat = conn.createStatement();
            resultSet = stat.executeQuery(sb.toString());

            while (resultSet.next()){
                BookType bookType1 = new BookType();
                bookType1.setId(resultSet.getInt("id"));
                bookType1.setType(resultSet.getString("type"));
                bookTypes.add(bookType1);
            }
            /*返回查找到的图书类型*/
            return bookTypes;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                JdbcUtils.close(conn,stat,resultSet);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    public List<BookType> findObjByPage(BookType bookType, int pageNum, int pageSize) {
        /*这个就不写了吧 感觉用不上*/
        return null;
    }
}
